Functions defined in the class are implicitly inline.
every member function must be declared inside its class.
Member functions access the object on which they were called through an extra, implicit parameter named this.
this is intended to always refer to “this” object(current function), this is a const pointer
const after the parameter list of a member function. A const following the parameter list indicates that this is a pointer to const
The name of a member defined outside the class must include the name of the class of which it is a member
Dedining a function to Return “this” Object
nonmember functions that are part of the interface of a class should be declared in the same header as the class itself.
Constructer
constructors may not be declared as const .When we create a const object of a class type, the object does not assume its “constness” until after the constructor completes the object’s initialization.
The compiler generates a default constructor automatically only if a class
declares no constructors
Class:use class to define the class are similar to struct ,the only difference is the default level.
A class may define members before the first access specifier.
To members before the first access specifier, for struct they are public,for class they are private.
private:accessible to the menber function not accessible to code that use the class.
A class allow another class or function to access its nonpublic menbers by making that class or function a friend.
Each class controls which classes or functions are its friends.friendship is not transitive.
a friend declaration affects access but is not a declaration in an ordinary sense,if we define the function inside the class, we must still provide a declaration outside of the class itself to make that function visible
We must use the constructor initializer list to provide values for members that are const, reference, or of a class type that does not have a default constructor.
Members are initialized in the order in which they appear in the class definition.
- It is a good idea to write constructor initializers in the same order as the
members are declared. Moreover, when possible, avoid using members to initialize other members. - it is a good idea write member initializers to use the constructor’s
parameters rather than another data member from the same object.
Only One Class-Type Conversion Is Allowed
* An aggregate class (§ 7.5.5, p. 298) whose data members are all of literal type is a literal class??
static Class Members
a member is associated with the class by adding the keyword static to its
declaration.
Even though static members are not part of the objects of its class, we can use an object, reference, or pointer of the class type to access a static member:
As with any class member, when we refer to a class static member outside
the class body, we must specify the class in which the member is defined.
The static keyword, however, is used only on the declaration inside the
class body.
we must define and initialize each static
data member outside the class body. and a static data member may be defined only once.
- a static data member can have incomplete type
- a static data member can have the same type as the class type of which
it is a member. - we can use a static member as a default argument